﻿using System;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Linq.Expressions;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using NPOI.SS.Formula.PTG;
using YiSha.Web.Code;

namespace YiSha.Service.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-12-29 18:01
    /// 描 述：Courses服务类
    /// </summary>
    public class CoursesService : RepositoryFactory
    {
        private CourseMediaService courseMediaService = new CourseMediaService();
        private CustomerUserService customerUserService = new CustomerUserService();
        #region 获取数据
        public async Task<List<CoursesEntity>> GetList(CoursesListParam param)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression);
            return list.ToList();
        }

        public async Task<List<CoursesEntity>> GetPageList(CoursesListParam param, Pagination pagination)
        {
            var expression = ListFilter(param);
            var list = await this.BaseRepository().FindList(expression, pagination);
            return list.ToList();
        }

        public async Task<CoursesDto> GetEntity(long id)
        {
            var data = await this.BaseRepository().FindEntity<CoursesEntity>(id);
            CoursesDto coursesDto = new CoursesDto();
            coursesDto.title = data.title;
            coursesDto.Id = data.Id;
            coursesDto.price = data.price;
            coursesDto.user_id = data.user_id;
            coursesDto.publish_time = data.publish_time;
            coursesDto.description = data.description;
            coursesDto.view_count = data.view_count;
            coursesDto.created_at = data.created_at;
            coursesDto.updated_at = data.updated_at;
            coursesDto.status = data.status;
            var mediaList = await courseMediaService.GetList(id);
            coursesDto.CourseMediaEntities = mediaList;
            return coursesDto;
        }
        #endregion

        #region 提交数据
        public async Task SaveForm(CoursesDto entity)
        {
            if (entity.Id.IsNullOrZero())
            {

                //OperatorInfo operatorInfo = await Operator.Instance.Current();

                CoursesEntity coursesEntity = new CoursesEntity();
                coursesEntity.title = entity.title;
                coursesEntity.price = entity.price;
                coursesEntity.description = entity.description;
                coursesEntity.status = entity.status;
                coursesEntity.created_at = DateTime.UtcNow;
                coursesEntity.updated_at = DateTime.UtcNow;
                coursesEntity.publish_time = entity.publish_time;
                coursesEntity.view_count = 0;

                //CustomerUserListParam param = new CustomerUserListParam();
                //param.openid = operatorInfo.openid;

                //var cuser = await customerUserService.GetList(param);


                coursesEntity.user_id = entity.user_id;// cuser.FirstOrDefault().Id;

                coursesEntity.Create();
                await this.BaseRepository().Insert(coursesEntity);

                List<CourseMediaEntity> courseMediaEntities = new List<CourseMediaEntity>();
                for (int i = 0; i < entity.CourseMediaEntities.Count; i++)
                {
                    entity.CourseMediaEntities[i].course_id = coursesEntity.Id;
                    entity.CourseMediaEntities[i].Create();
                    courseMediaEntities.Add(entity.CourseMediaEntities[i]);
                }

                await courseMediaService.SaveForm(courseMediaEntities);
            }
            else
            {
                CoursesEntity coursesEntity = new CoursesEntity();
                coursesEntity.Id = entity.Id;
                coursesEntity.title = entity.title;
                coursesEntity.price = entity.price;
                coursesEntity.description = entity.description;
                coursesEntity.status = entity.status;
                coursesEntity.updated_at = DateTime.UtcNow;
                coursesEntity.publish_time = entity.publish_time;

                await this.BaseRepository().Update(coursesEntity);

                List<CourseMediaEntity> courseMediaEntities = new List<CourseMediaEntity>();
                for (int i = 0; i < entity.CourseMediaEntities.Count; i++)
                {
                    entity.CourseMediaEntities[i].course_id = coursesEntity.Id;
                    entity.CourseMediaEntities[i].Create();
                    entity.CourseMediaEntities[i].created_at = DateTime.UtcNow;
                    entity.CourseMediaEntities[i].updated_at = DateTime.UtcNow;
                    entity.CourseMediaEntities[i].Create();
                    courseMediaEntities.Add(entity.CourseMediaEntities[i]);
                }
                long Id = 0;
                if (entity.Id > 0)
                {
                    Id = (long)entity.Id;
                }
                var list = await courseMediaService.GetList(Id);
                string ids = string.Join(",", list.Select(x => x.Id).ToList());
                if(!string.IsNullOrEmpty(ids))
                { 
                    await courseMediaService.DeleteForm(ids);
                }
                 
                await courseMediaService.SaveForm(courseMediaEntities);
            }
        }

        public async Task DeleteForm(string ids)
        {
            long[] idArr = TextHelper.SplitToArray<long>(ids, ',');
            await this.BaseRepository().Delete<CoursesEntity>(idArr);
        }
        #endregion

        #region 私有方法
        private Expression<Func<CoursesEntity, bool>> ListFilter(CoursesListParam param)
        {
            var expression = LinqExtensions.True<CoursesEntity>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.title))
                {
                    expression = expression.And(t => t.title == param.title);
                }
            }
            return expression;
        }
        #endregion
    }
}
